package top.blentle.ai.data;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.jms.Topic;

/**
 * @author renhuan
 * @desc
 */
@Configuration
public class AiContext {
    /**
     * websocket endpoint
     * @return
     */
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }

    @Value("${topic.report.name}")
    private String reportTopic;


    @Value("${topic.dispatch.name}")
    private String dispatchTopic;

    @Value("${spring.activemq.broker-url}")
    private  String brokerUrl;


    @Bean
    public Topic reportTopic(){
        return new ActiveMQTopic(reportTopic);
    }

    @Bean
    public Topic dispatchTopic(){
        return new ActiveMQTopic(dispatchTopic);
    }

    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
        return new ActiveMQConnectionFactory(brokerUrl);
    }

    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){
        DefaultJmsListenerContainerFactory jmsListenerContainerTopic = new DefaultJmsListenerContainerFactory();
        //设置为发布订阅方式, 默认情况下使用的生产消费者方式
        jmsListenerContainerTopic.setPubSubDomain(true);
        jmsListenerContainerTopic.setConnectionFactory(connectionFactory);
        return jmsListenerContainerTopic;
    }
}
